<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
	
// 	默认主页为登录页面
	public function index() {
		$this->display ( 'login' );
	}
	
	public function doLoginByAndroid() {
		$userName = $_POST ['username'];
		$passWord = $_POST ['password'];
		
		$model = M ( 'inspector' );
		$where ['name'] = $userName;
		$where ['password'] = $passWord;
		
		$result = $model->where ( $where )->select ();
		
		if ($result) {
			echo ("success");
		} else {
			echo ("error");
		}
	}
	
	/**
	 * 网页端登录方法
	 */
	public function doLoginByWeb() {
			
		//判断登录端传过来的用户名和密码是否为空
		if (empty ( $_POST ['password'] ) || empty ( $_POST ['username'] )) {
			$this->assign ( 'jumpUrl', U ( 'Index/index' ) );
			$this->error ( '密码不能为空' );
		}
		
		$userName = $_POST ['username'];
		$passWord = $_POST ['password'];
		
		unset ( $_POST ['username'], $_POST ['password'], $_SESSION ['admin_id'], $_SESSION ['admin_name'], $_SESSION ['isLogin'] );
		
		$_SESSION['isLogin'] = NULL;
		$model = D ( 'admin' );
		$where ['name'] = $userName;
		$where ['password'] = $passWord;
		
		$result = $model->where ( $where )->select ();
		
		if ($result) {
			session('admin_id', $result [0] ['admin_id']);
			$_SESSION ['admin_id'] = $result [0] ['admin_id'];
			$_SESSION ['admin_name'] = $result [0] ['name'];
			$_SESSION ['admin_type'] = $result [0] ['s4'];
			$_SESSION ['isLogin'] = TRUE;
		}
		if ($_SESSION ['isLogin'] == TRUE) {
			$this->assign ( 'jumpUrl', U ( 'Index/mapsDemo' ) );
			$this->success ( "登录成功" );
		} else {
			$this->assign ( 'jumpUrl', U ( 'Index/index' ) );
			$this->error ( '密码或用户名错误' );
		}
	}
	
	
	function exitLogin() {
		$_SESSION ['isLogin'] = "notLogin";
		unset ( $_POST ['username'], $_POST ['password'], $_SESSION ['admin_id'], $_SESSION ['admin_name'], $_SESSION ['isLogin'] );
		$this->assign ( 'jumpUrl', U ( 'Index/index' ) );
		$this->success ( "成功退出" );
	}
	public function getGpsData() {
		$model = M ( 'inspection_data' );
		$list = $model->where ()->order ( 'id desc' )->limit ( 1 )->select ();
		$latitude = $list [0] ["latitude"];
		$longitude = $list [0] ["longitude"];
		$time = $list [0] ["time"];
		$array1 = array (
				'latitude' => $latitude,
				'longitude' => $longitude,
				'time' => $time 
		);
		echo json_encode ( $array1 );
	}
	
	public function mapsDemo() {
		if ($_SESSION ["isLogin"] == "isLogin") {
			$this->display ();
		} else {
			$this->assign ( 'jumpUrl', U ( 'Index/index' ) );
			$this->error ( '请先登录' );
		}
	}
	
	public function loadGPS() {
		$data ['latitude'] = $_POST ['latitude'];
		$data ['longitude'] = $_POST ['longitude'];
		$data ['inspector_id'] = $_POST ['inspector_id'];
		$data ['time'] = $_POST ['time'];
		
		$model = M ( 'inspection_data' );
		$result = $model->data ( $data )->add ();
		
		$GLOBALS ['latitude'] = $data ['latitude'];
		$GLOBALS ['longitude'] = $data ['longitude'];
	}
	
	/**
	 * 从数据库中读取所有的管线数据，并以JSON格式输出
	 */
	public function loadLines() {
		$model = D ( 'lines' );
		$result = $model->select ();
		// var_dump($result);
		$array1 = array (
				'line_id',
				'name',
				'inspector_id',
				'node_amount',
				'last_edit_time',
				'admin_id' 
		);
		$array2 = array (
				'',
				'',
				'',
				'',
				'',
				'' 
		);
		$array3 = array ();
		
		foreach ( $result as $key => $value ) {
			$array2 [0] = $value ['line_id'];
			$array2 [1] = $value ['name'];
			$array2 [2] = $value ['inspector_id'];
			$array2 [3] = $value ['node_amount'];
			$array2 [4] = $value ['last_edit_time'];
			$array2 [5] = $value ['admin_id'];
			$arraytem = array_combine ( $array1, $array2 );
			array_push ( $array3, $arraytem );
		}
		$array_lines = array (
				'lines' => $array3 
		);
		echo json_encode ( $array_lines );
	}
	public function loadNodes() {
		$line_id = $_GET ['line_id'];
		// $line_id = 0;
		$model = D ( 'node' );
		$where = array (
				'line_id' => $line_id 
		);
		$result = $model->field ( 'name,longitude,latitude,order_id' )->where ( $where )->order ( 'order_id' )->select ();
		// var_dump($result);
		// die();
		$array1 = array (
				'name',
				'longitude',
				'latitude',
				'order_id' 
		);
		$array2 = array (
				'',
				'',
				'',
				'' 
		);
		$array3 = array ();
		
		foreach ( $result as $key => $value ) {
			$array2 [0] = $value ['name'];
			$array2 [1] = $value ['longitude'];
			$array2 [2] = $value ['latitude'];
			$array2 [3] = $value ['order_id'];
			$arraytem = array_combine ( $array1, $array2 );
			array_push ( $array3, $arraytem );
		}
		$array_nodes = array (
				'nodes' => $array3 
		);
		echo json_encode ( $array_nodes );
	}
	public function test() {
		$this->display ();
	}
}


